package com.xiaoxin.experience.api.tree;

import com.xiaoxin.experience.common.RestResponse;
import com.xiaoxin.experience.service.tree.ApiTreeService;
import com.xiaoxin.experience.service.tree.common.JsTree;
import com.xiaoxin.experience.service.tree.model.Api;
import com.xiaoxin.experience.service.tree.model.ApiDirectory;
import com.xiaoxin.experience.util.StringUtil;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author xiaoxin
 */
@RestController
@RequestMapping("/api/tree")
public class ApiTreeController {

    @Resource
    ApiTreeService apiTreeService;


    @RequestMapping("/select")
    public RestResponse<List<JsTree>> selectApiTree()
    {
        return RestResponse.success(apiTreeService.selectApiTree());
    }

    @RequestMapping("/createApi")
    public RestResponse<Void> createApi(@RequestBody Api api)
    {
        apiTreeService.createApi(api);
        return RestResponse.success();
    }

    @RequestMapping("/updateApi")
    public RestResponse<Void> updateApi(@RequestBody Api api)
    {
        apiTreeService.updateApi(api);
        return RestResponse.success();
    }

    @RequestMapping("/deleteApi")
    public RestResponse<Void> deleteApi(@RequestBody Api api)
    {
        String id = api.getId();
        if (StringUtil.isBlank(id))
        {
            return RestResponse.success();
        }
        apiTreeService.deleteApi(id);
        return RestResponse.success();
    }

    @RequestMapping("/createApiDirectory")
    public RestResponse<Void> createApiDirectory(@RequestBody ApiDirectory apiDirectory)
    {
        apiTreeService.createDirectory(apiDirectory);
        return RestResponse.success();
    }

    @RequestMapping("/deleteApiDirectory")
    public RestResponse<Void> deleteApiDirectory(@RequestBody ApiDirectory apiDirectory)
    {
        String id = apiDirectory.getId();
        if (StringUtil.isBlank(id))
        {
            return RestResponse.success();
        }
        return apiTreeService.deleteDirectory(id);
    }
}
